package com.houtai.service.job;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TdJob;
import com.houtai.bean.dto.job.TdJobListDto;
import com.houtai.bean.dto.user.job.JobUserListDto;
import com.houtai.bean.vo.user.job.JobUserList;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TdJobDao;
import com.houtai.dao.TdUserJobDao;
import com.houtai.dao.TsJzAdminOperationDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JobService {
    private static Logger logger = Logger.getLogger(JobService.class);
    @Autowired
    private TdJobDao tdJobDao;
    @Autowired
    private TdUserJobDao tdUserJobDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;
    /**
     * @Purpose   查找岗位列表
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findTdJob(TdJobListDto page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findTdJob:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "update_time desc");
            List<TdJob> lists = tdJobDao.findTdJob(page);
            PageInfo<TdJob> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());

            map.put("company_id", page.getCompany_id());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找岗位列表" , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }
    /**
     * @Purpose   查找岗位投递记录
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo jobUserList(JobUserListDto page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":jobUserList:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "j_u_id desc");
            List<JobUserList> lists = tdUserJobDao.findJobUserList(page.getJob_id());
            PageInfo<JobUserList> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("job_id", page.getJob_id());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找岗位投递记录" , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }
}
